﻿@{
    ViewBag.Title = "Home Page";
}

<div>
    <a href="javascript:Snake.START()">Start</a> | 
    <a href="javascript:Snake.Stop()">Stop</a> | Or press "Space"
</div>
<div id="canvas_container" style="width:1000px;height:200px;border:1px solid red;margin:0px;padding:0px;"></div>
<div>
    Score : <span id="snake-score"></span> |
    Time : <span id="snake-time"></span> |
    <span id="snake-message"></span>
</div>
<script type="text/javascript">
    var Position = function(left,top) {
        this.X = left;
        this.Y = top;
    }

   var BodyPart = function (position, size, fill, stroke)
   {
        var aux = Game.Paper.rect(position.X, position.Y, size || Game.DefaultUnitSize, size || Game.DefaultUnitSize);
        aux.attr("fill", fill || '#000');
        aux.attr("stroke", stroke || '#000');
        aux.direction = Direction.Right;
        return aux;
    }

    var Game =
        {
            CanvasBoundries : 
                {
                    left: 0,
                    right: 0,
                    top: 0,
                    bottom : 0
                },
            Width: 1000,
            Height: 200,
            DefaultUnitSize : 20,
            Speed : 300,
            Paper: null,
            Rect: null,
            Food: null,
            Init: function ()
            {
                //calculate boundries
                var container = $('#canvas_container');
                Game.CanvasBoundries.top = container.position().top;
                Game.CanvasBoundries.left = container.position().left;
                Game.CanvasBoundries.right = container.position().left + container.width();
                Game.CanvasBoundries.bottom = container.position().top + container.height();

                //init raphael
                Game.Paper = new Raphael(document.getElementById('canvas_container'), Game.Width, Game.Height);

                //pt tastat
                // left - 37
                // up  - 38
                // right - 39
                // doown - 40
                //32 space
                $(document).keydown(function (e) {
                    switch (e.keyCode) {
                        case 39: MoveRight(); break;
                        case 37: MoveLeft(); break;
                        case 38: MoveUp(); break;
                        case 40: MoveDown(); break;
                        case 32: SpacePress(); break;
                    }
                });

            },
            GetRandomPosition: function () {
                var randomX = Math.abs(Math.floor(Math.random() * ((Game.Width/Game.DefaultUnitSize))-1) * Game.DefaultUnitSize);
                var randomY = Math.abs(Math.floor(Math.random() * ((Game.Height / Game.DefaultUnitSize))-1) * Game.DefaultUnitSize);
                return new Position(randomX, randomY);
            },
            SpawnPart: function () {
                var  p  = Game.GetRandomPosition();
                var next = new BodyPart(p, Game.DefaultUnitSize, '#0000FF');
                Game.Food = next;
            }

        }


    var Direction =
    {
        'right': { x: Game.DefaultUnitSize, y: 0 },
        'left': { x: -1*Game.DefaultUnitSize, y: 0 },
        'up': { x: 0, y: -1*Game.DefaultUnitSize },
        'down': { x: 0, y: Game.DefaultUnitSize }
    }


    function TimeToString(time) {
        var minutes = Math.floor(time / 1000 / 60);
        var seconds = Math.floor((time-(minutes*6000)) / 1000);
        return (minutes<10?'0'+minutes:minutes)+':'+ (seconds < 10 ? '0'+seconds : seconds);
    }



    var Snake = {
        Body: [],
        Score: 0,
        Time : null,
        isMoving : false,
        Add: function (part)
        {
            Snake.Body.push(part);
        },
        CalculateSpeed: function () {
            if (Snake.Score % 5 == 0) {
                Game.Speed = Game.Speed-10;
            }
            
        },
        CheckIfFoundFood: function (nextX, nextY) {
            if (Game.Food != null) {
                var foodX = Game.Food.attrs.x;
                var foodY = Game.Food.attrs.y;
                if (parseInt(foodX - 20) < parseInt(nextX) &&
                        parseInt(nextX) < parseInt(foodX + 20) &&
                        parseInt(foodY - 20) < parseInt(nextY) &&
                        parseInt(nextY) < parseInt(foodY + 20)
                    )
                {
                    Snake.Score += 1;
                    Game.Food.remove();
                    Snake.CalculateSpeed();
                    var newX = parseInt(Snake.Body[Snake.Body.length-1].attrs.x)+Game.DefaultUnitSize;
                    var newY = parseInt(Snake.Body[Snake.Body.length - 1].attrs.y);

                    var bPart = new BodyPart(new Position(newX, newY), Game.DefaultUnitSize);
                    Snake.Add(bPart);
                    bPart = null;

                    Game.SpawnPart();
                    Snake.ShowResults();
                };
            }            
        },
        StartMove: function (dir) {
            for (var i = 0; i < Snake.Body.length; i++)
            {
                var part = Snake.Body[i];
                Snake.CheckIfFoundFood(part.attrs.x, part.attrs.y);
                Snake.CheckIfGameOver(part.attrs.x, part.attrs.y);
                var anim = Raphael.animation({
                    x: part.attrs.x + Direction[dir].x,
                    y: part.attrs.y + Direction[dir].y
                }, Game.Speed, 'linear',
                function ()
                {
                    Snake.StartMove(dir);
                });
                part.stop().animate(anim);
            }
        },
        Stop : function () {
            for (var i = 0; i < Snake.Body.length; i++) {
                Snake.Body[i].stop();
                Snake.Body[i].remove();
            }         
            Snake.Body.splice(0);
            Snake.isMoving = false;
            Snake.Time = new Date() - Snake.Time;
            Game.Food.remove();
            Game.Food = null;
            Snake.ShowResults();
          
        },
        START: function () {
            //clear old results
            Snake.ClearResults();

            Game.Speed = 300;

            //add 3 blocs to snake
            var p1 = new BodyPart(new Position(20, 80));
            var p2 = new BodyPart(new Position(40, 80));
            var p3 = new BodyPart(new Position(60, 80));
            Snake.Add(p1);
            Snake.Add(p2);
            Snake.Add(p3);

            //spawn prize
            if (Game.Food == null) {
                Game.SpawnPart();
            }
            //start movement
            Snake.isMoving = true;
            Snake.Time = new Date();
            Snake.StartMove('right');
        },
        ColorHead : function () {            
            Snake.Body[Snake.Body.length - 1].attr('fill', '#FF0000')
        },
        ClearResults : function() {
            $('#snake-score').html('');
            $('#snake-time').html('');
            $('#snake-message').html('');
        },
        ShowResults: function() {
            $('#snake-score').html(Snake.Score);
            if (!Snake.isMoving) {
                $('#snake-time').html(TimeToString(Snake.Time));
                $('#snake-message').html('Game Over! :)');
            }
            
        },
        CheckIfGameOver: function (x,y)
        {
            if (x <= 0 || y <= 0)
            {
                Snake.Stop();
            }
        }
    }

   




    function MoveRight()
    {
        Snake.StartMove('right');
    }
    function MoveLeft() {
        Snake.StartMove('left');
    }
    function MoveUp() {
        Snake.StartMove('up');

    }
    function MoveDown() {
        Snake.StartMove('down');
    }
    function SpacePress() {
        if (Snake.isMoving) {
            Snake.Stop();
        }
        else {
            Snake.START();
        }

    }




    $(function () {
        Game.Init();
    })
</script>


